//
//  RankViewController.m
//  YLProject
//
//  Created by 王跃 on 2018/1/10.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "RankViewController.h"
#import "WY_ScrollButtonView.h"
#import "Rank_TalkAboutController.h"
@interface RankViewController ()<UIScrollViewDelegate,
                                 WY_ScrollButtonViewDelegate
                                >

@property (nonatomic ,strong) WY_ScrollButtonView         *scrollButtonView;
@property (nonatomic ,strong) UIScrollView                *scrollView;
@property (nonatomic ,strong) UIButton *leftButton;
@end

@implementation RankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    [self setUpRankTitleView];
    [self setUpScrollView];
    //添加子控制器
    [self addChildViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - UI
-(void)setUpNav{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 5,34,34);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"排行_highlight"] forState:UIControlStateNormal];
    [[rightButton  rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"排行右按钮");
        Rank_TalkAboutController *vc = [[Rank_TalkAboutController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc]initWithCustomView:rightButton]];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = CGRectMake(0, 5,34,34);
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"排行_highlight"] forState:UIControlStateNormal];
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"排行"] forState:UIControlStateSelected];
    [[_leftButton  rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"排行左按钮");
        _leftButton.selected = !_leftButton.selected;
    }];
    _leftButton.hidden = YES;
    self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc]initWithCustomView:_leftButton]];
}
- (void)setUpRankTitleView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.scrollButtonView.frame = CGRectMake(0,
                                             0,
                                             Screen_Width * 3 / 4,
                                             44);
    self.navigationItem.titleView = self.scrollButtonView;
}

//顶部导航Title
-(WY_ScrollButtonView *)scrollButtonView
{
    if (!_scrollButtonView)
    {
        _scrollButtonView = [[WY_ScrollButtonView alloc]init];
        _scrollButtonView.buttonTitleArray = @[@"热门",
                                               @"在线",
                                               @"魅力",
                                               @"财气",
                                               @"同城"];
        _scrollButtonView.tintColor = [UIColor redColor];
        _scrollButtonView.defaultcolor = [UIColor blackColor];
        _scrollButtonView.isLengthContainTitle = YES;
        _scrollButtonView.backgroundColor = [UIColor clearColor];
        _scrollButtonView.delete = self;

    }
    return _scrollButtonView;
}

- (void)setUpScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.scrollView.bounces = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor colorWithRed:230/255.0
                                                      green:230/255.0
                                                       blue:230/255.0
                                                      alpha:0.1];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 5,
                                             0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];

}

- (void)addChildViewController
{
    NSArray *array = @[@"Rank_HotViewController",
                       @"Rank_OnlineViewController",
                       @"Rank_LovingViewController",
                       @"Rank_RichesViewController",
                       @"Rank_CommonCityViewController",
//                       @"Rank_NewPersonViewController"
                       ];
    for (int i = 0; i < array.count; i++)
    {
        UIViewController *vc = [[NSClassFromString(array[i]) alloc]init];
        [self addChildViewController:vc];
    }

    [self scrollViewDidEndDecelerating:self.scrollView];
}

#pragma mark  顶部导航delegate

-(void)buttonSelectedIndex:(NSInteger)index
{
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width * index, 0)
                             animated:YES];
    if (index == 1 || index == 4) {
        _leftButton.hidden = NO;
    }else{
        _leftButton.hidden = YES;
    }
}

#pragma mark - RankTableViewProtocol


#pragma mark - UIScrollViewDelegate

//滑动结束添加view
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;

    //获取索引
    NSInteger scrollIndex = offsetX / width;
    [self.scrollButtonView ScrollButtonViewDidScrollToIndex:scrollIndex];
    UIViewController *vc = self.childViewControllers[scrollIndex];
    if ([vc isViewLoaded]) return;
    CGRect rect = CGRectMake(offsetX,
                             0,
                             width,
                             height);
    [vc.view setFrame:rect];
    [self.scrollView addSubview:vc.view];
}

//scrollview减速结束时调用主控制器view
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}



@end
